package co.edu.usbcali.prototipobeta.presentation.backingBeans;

import co.edu.usbcali.prototipobeta.exceptions.ZMessManager;
import co.edu.usbcali.prototipobeta.modelo.Bebe;
import co.edu.usbcali.prototipobeta.modelo.Usuario;
import co.edu.usbcali.prototipobeta.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.prototipobeta.utilities.FacesUtils;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;

public class AgregarMedicamentoView {
	
	private HtmlInputText txtBebeId_Bebe  = new HtmlInputText();
	private HtmlInputText txtMedId  = new HtmlInputText();
	private HtmlInputText txtDetallMedicId  = new HtmlInputText();
	
	private HtmlInputText txtMedNombre = new HtmlInputText();
	private HtmlInputText txtDescripcion = new HtmlInputText();
   
    
    private HtmlCommandButton btnSave;
    private HtmlCommandButton btnClear;
    
    
       
	public AgregarMedicamentoView() {
		super();
	}
	
	
	
	//Metodo cancelar o limpiar
	public String action_Cancelar() {
		txtBebeId_Bebe.setValue(null);
		txtMedId.setValue(null);
		txtDetallMedicId.setValue(null);
		txtMedNombre.setValue(null);
		txtDescripcion.setValue(null);

        return "";
    }
	
	//Metodo que guarda en medicina y detalle medicamento
	public String action_save() {
        try {
        	
        	Usuario usu = (Usuario) FacesUtils.getSession().getAttribute("usuario");
        	Bebe bebe = BusinessDelegatorView.consultarBebePorUsuarioID(usu.getUsuId());
        	
        	Long medicianID = BusinessDelegatorView.findTotalNumberMedicina()+1;
            BusinessDelegatorView.saveMedicina(medicianID,FacesUtils.checkString(txtMedNombre),bebe.getBebeId());
            BusinessDelegatorView.saveDetalleMedicina(FacesUtils.checkString(txtDescripcion), medicianID,medicianID);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_Cancelar();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }
	
	
	public HtmlInputText getTxtBebeId_Bebe() {
		return txtBebeId_Bebe;
	}
	public void setTxtBebeId_Bebe(HtmlInputText txtBebeId_Bebe) {
		this.txtBebeId_Bebe = txtBebeId_Bebe;
	}
	public HtmlInputText getTxtMedId() throws Exception {
		Long medicianID = BusinessDelegatorView.findTotalNumberMedicina()+1;
		txtMedId.setValue(medicianID);
		return txtMedId;
	}
	public void setTxtMedId(HtmlInputText txtMedId) {
		this.txtMedId = txtMedId;
	}
	public HtmlInputText getTxtDetallMedicId() {
		return txtDetallMedicId;
	}
	public void setTxtDetallMedicId(HtmlInputText txtDetallMedicId) {
		this.txtDetallMedicId = txtDetallMedicId;
	}
	public HtmlInputText getTxtMedNombre() {
		return txtMedNombre;
	}
	public void setTxtMedNombre(HtmlInputText txtMedNombre) {
		this.txtMedNombre = txtMedNombre;
	}
	public HtmlInputText getTxtDescripcion() {
		return txtDescripcion;
	}
	public void setTxtDescripcion(HtmlInputText txtDescripcion) {
		this.txtDescripcion = txtDescripcion;
	}
	public HtmlCommandButton getBtnSave() {
		return btnSave;
	}
	public void setBtnSave(HtmlCommandButton btnSave) {
		this.btnSave = btnSave;
	}
	public HtmlCommandButton getBtnClear() {
		return btnClear;
	}
	public void setBtnClear(HtmlCommandButton btnClear) {
		this.btnClear = btnClear;
	}

    
    
    
    
}
